package com.hoshiicloud.common.feign;

import com.hoshiicloud.common.feign.fallback.BaseHystrixFileObjectFeignFallback;
import com.hoshiicloud.common.query.UploadFileResponse;
import com.hoshiicloud.common.rpc.Response;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author:campchen
 * @Date:Created in 下午 02:13 2019/9/16
 * @PACKAGENAME com.hoshiicloud.suppplier.feign
 * @Description
 */
@FeignClient(name = "hoshiicloud-fileserver", fallback = BaseHystrixFileObjectFeignFallback.class)
@Repository
public interface BaseFileObjectFeignService {

    /**
     * 上传
     *
     * @param bucketName
     * @param objectKey
     * @param fileName
     * @param folder
     * @param privateOwned
     * @param file
     * @return
     */
    @RequestMapping(value = "/file/uploadFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "feign上传一个文件", notes = "传入已存在objectKey时将会覆盖原文件", hidden = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", value = "存储空间名称，上传时指定的存储空间", paramType = "String", example = "obs-7d37"),
            @ApiImplicitParam(name = "objectKey", value = "文件key,上传时存储的对象key,如果key相同并且文件已存在，将覆盖文件，上传完毕后原样返回，如不传则使用文件MD5值。", paramType = "String"),
            @ApiImplicitParam(name = "fileName", value = "文件名,批量上传时需和key对应", paramType = "String"),
            @ApiImplicitParam(name = "folder", value = "文件夹路径", paramType = "String"),
            @ApiImplicitParam(name = "privateOwned", value = "对象私有读写,因没有设置的权限暂时无效", paramType = "Boolean", example = "false"),
            @ApiImplicitParam(name = "file", value = "文件", paramType = "formData", dataType = "MultipartFile"),
    })
    Response<UploadFileResponse> uploadFile(@RequestParam("bucketName") String bucketName, @RequestParam("objectKey") String objectKey, @RequestParam("fileName") String fileName, @RequestParam("folder") String folder, @RequestParam("privateOwned") Boolean privateOwned, MultipartFile file);

    /**
     * 下载
     *
     * @param bucketName
     * @param objectKey
     * @param response
     */
    @RequestMapping(value = "/file/downloadFile", method = RequestMethod.GET, produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "下载一个文件", notes = "返回文件对象")
    void downloadFile(@RequestParam("bucketName") String bucketName, @RequestParam("objectKey") String objectKey, HttpServletResponse response);
}
